﻿using System.IO;
using System.Xml.Serialization;

namespace Infrastructure.Common.Config.Imp
{
    /// <summary>
    ///     菜鸟，景天
    ///     采用xml序列化的方式保存和加载配置文件
    /// </summary>
    public class XmlManager : IConfigManage
    {
        public void Save<T>(T entity, string filePath)
        {
            using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                var serialize = new XmlSerializer(typeof (T));
                serialize.Serialize(fs, entity);
                fs.Close();
            }
        }

        public void Load<T>(string filePath, out T entity)
        {
            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var serialize = new XmlSerializer(typeof (T));
                entity = (T) serialize.Deserialize(fs);
                fs.Close();
            }
        }
    }
}